package com.john.waveview;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by John on 2014/10/15.
 */
public class WaveView extends DirectionalLayout implements Component.DrawTask {
    protected static final int LARGE = 1;
    protected static final int MIDDLE = 2;
    protected static final int LITTLE = 3;

    private final Color DEFAULT_ABOVE_WAVE_COLOR = Color.WHITE;
    private final Color DEFAULT_BLOW_WAVE_COLOR = Color.WHITE;
    private final int DEFAULT_PROGRESS = 80;

    private Color mAboveWaveColor = DEFAULT_ABOVE_WAVE_COLOR;
    private Color mBlowWaveColor = DEFAULT_BLOW_WAVE_COLOR;
    private int mProgress = DEFAULT_PROGRESS;
    private int mWaveHeight = MIDDLE;
    private int mWaveMultiple = LARGE;
    private int mWaveHz = MIDDLE;

    private int mWaveToTop;

    private Wave mWave;
    private Solid mSolid;


    public WaveView(Context context, AttrSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        //load styled attributes.
        if (attrs.getAttr("above_wave_color").isPresent()) {
            mAboveWaveColor = attrs.getAttr("above_wave_color").get().getColorValue();
        }
        if (attrs.getAttr("blow_wave_color").isPresent()) {
            mBlowWaveColor = attrs.getAttr("blow_wave_color").get().getColorValue();
        }
        if (attrs.getAttr("progress").isPresent()) {
            mProgress = attrs.getAttr("progress").get().getIntegerValue();
        }
        if (attrs.getAttr("wave_height").isPresent()) {
            mWaveHeight = attrs.getAttr("wave_height").get().getIntegerValue();
        }
        if (attrs.getAttr("wave_length").isPresent()) {
            mWaveMultiple = attrs.getAttr("wave_length").get().getIntegerValue();
        }
        if (attrs.getAttr("wave_hz").isPresent()) {
            mWaveHz = attrs.getAttr("wave_hz").get().getIntegerValue();
        }

        mWave = new Wave(context, null);
        mWave.initializeWaveSize(mWaveMultiple, mWaveHeight, mWaveHz);
        mWave.setAboveWaveColor(mAboveWaveColor);
        mWave.setBlowWaveColor(mBlowWaveColor);
        mWave.initializePainters();

        mSolid = new Solid(context, null);
        mSolid.setAboveWavePaint(mWave.getAboveWavePaint());
        mSolid.setBlowWavePaint(mWave.getBlowWavePaint());

        addComponent(mWave);
        addComponent(mSolid);

        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (getWidth() != 0) {
            setProgress(mProgress);
        }
    }

    public void setProgress(int progress) {
        this.mProgress = progress > 100 ? 100 : progress;
        computeWaveToTop();
    }

    private void computeWaveToTop() {
        mWaveToTop = (int) (getHeight() * (1f - mProgress / 100f));
        ComponentContainer.LayoutConfig params = mWave.getLayoutConfig();
        if (params != null) {
            params.setMarginTop(mWaveToTop);
        }
        mWave.setLayoutConfig(params);
    }

}
